package com.shortvedio.search.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shortvedio.base.mapper.CommentsMapper;
import com.shortvedio.base.mapper.SUserInfoMapper;
import com.shortvedio.base.model.Result;
import com.shortvedio.base.model.dto.CommentsDto;
import com.shortvedio.base.model.po.Comments;
import com.shortvedio.base.model.po.SUserInfo;
import com.shortvedio.search.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentsServiceImpl implements CommentsService {

    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private SUserInfoMapper sUserInfoMapper;

    @Override
    public Result getRootCommentsByVideoId(Long videoId) {
//        LambdaQueryWrapper<Comments> commentsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //通过视频id获取
        List<CommentsDto> parentComment = commentsMapper.getParentComment(videoId);
        //渲染用户数据
        getUserInfo(parentComment);
        return Result.success(parentComment);
    }

    private void getUserInfo(List<CommentsDto> commentsDtos){//渲染用户数据到评论列表中
        commentsDtos.forEach(commentsDto -> {
            Long userId = commentsDto.getUserId();
            LambdaQueryWrapper<SUserInfo> sUserInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sUserInfoLambdaQueryWrapper.eq(SUserInfo::getUserId,userId);
            SUserInfo sUserInfo = sUserInfoMapper.selectOne(sUserInfoLambdaQueryWrapper);
            commentsDto.setUserName(sUserInfo.getName());
            commentsDto.setHeadSrc(sUserInfo.getHeadSrc());
        });
    }


    @Override
    public Result getReplyComments(Long videoId, Long parenId) {//获取回复评论
        List<CommentsDto> replyComments = commentsMapper.getReplyComments(videoId, parenId);
        getUserInfo(replyComments);
        return Result.success(replyComments);
    }
}
